﻿using ESRI.ArcGIS.ConversionTools;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geoprocessor;
using System;
using System.IO;
using System.Windows.Forms;

namespace ComponentGIS.View
{
    public partial class FormCADToGeodatabase : Form
    {
        public FormCADToGeodatabase()
        {
            InitializeComponent();
        }

        DialogResult CADDialogResult { get; set; }
        string DBName { get; set; }
        string DBPath { get; set; }
        string ExtensionName { get; set; }

        private void btnGeodatabase_Click(object sender, EventArgs e)
        {
            bool isExist = true;
            IWorkspaceFactory pWorkspaceFactory;
            if (radioMDB.Checked == true)   //个人地理数据库
            {
                ExtensionName = ".mdb";//拓展名
                pWorkspaceFactory = new AccessWorkspaceFactoryClass();
                sfdGeodatabase.FileName = txtGeodatabaseName.Text;
                if (sfdGeodatabase.ShowDialog() == DialogResult.OK)
                {
                    int index = sfdGeodatabase.FileName.LastIndexOf("\\");
                    DBPath = sfdGeodatabase.FileName.Substring(0, index);
                    DBName = Path.GetFileNameWithoutExtension(sfdGeodatabase.FileName);
                    isExist = File.Exists(sfdGeodatabase.FileName);
                }
            }
            else //文件地理数据库
            {
                ExtensionName = ".gdb";//拓展名
                pWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
                if (fbdGeodatabase.ShowDialog() == DialogResult.OK)
                {
                    isExist = pWorkspaceFactory.IsWorkspace(fbdGeodatabase.SelectedPath);
                    if (isExist)
                    {
                        int index = fbdGeodatabase.SelectedPath.LastIndexOf("\\");
                        DBPath = fbdGeodatabase.SelectedPath.Substring(0, index);
                        DBName = fbdGeodatabase.SelectedPath.Substring(index + 1);
                    }
                    else
                    {
                        DBPath = fbdGeodatabase.SelectedPath;
                        DBName = txtGeodatabaseName.Text;
                    }
                }
            }
            if (!isExist)
                pWorkspaceFactory.Create(DBPath, DBName, null, 0);
        }

        private void btnCAD_Click(object sender, EventArgs e)
        {
            CADDialogResult = ofdCAD.ShowDialog();
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            try
            {
                //1 异常处理
                if (DBPath == string.Empty || DBPath == null)
                {
                    MessageBox.Show("请先选择/创建地理数据库!", "警告",
                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    DialogResult = DialogResult.None;//清除关闭对话框
                    return;
                }
                //2 开始转换
                if (CADDialogResult == DialogResult.OK)
                {
                    this.Cursor = Cursors.WaitCursor;//转换中...
                    Geoprocessor gp = new Geoprocessor();
                    CADToGeodatabase tool = new CADToGeodatabase();
                    for (int i = 0; i < ofdCAD.FileNames.Length; i++)
                    {
                        tool.input_cad_datasets = ofdCAD.FileNames[i];
                        tool.out_dataset_name = txtGeodatasetName.Text + (i + 1).ToString();
                        tool.out_gdb_path = DBPath + "\\" + DBName + ExtensionName;
                        tool.reference_scale = 1000;
                        gp.Execute(tool, null);
                    }
                    MessageBox.Show("转换成功！", "提示",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("请先选择CAD数据!", "警告",
                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    DialogResult = DialogResult.None;//清除关闭对话框
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}